jsx3.lang.Class.defineClass("ux.cdb.admin.CommonCache", null, null, 
    function(cache) {

        cache.serverCache = null;
        cache.commonCache = {};

        cache._instance = null;
		cache._selectProperty = '--Select--';
        
        cache.getInstance = function(){
            if(!this._instance) {
                this._instance = new cache(); 
            }
            return this._instance;
        };
        
        cache.prototype.cacheIdCount = 0;
        cache.prototype.cacheIdStep = 0;
        cache.prototype.callbackScope = this;
        cache.prototype.callbackFunction = function(){};
        
        /**
	        * Load common data from cache. you can update this class by your self.
	        * e.g. if you want load category1 and category2 to your page,use this:
	        *   ux.cdb.admin.CommonCache.loadCache(['Category1','Category2'],this,function(){
	        *       //this is your call back function.
	        *   });
	        * and your cacheid is 'CommonCache_**'
	        * @params cacheIds(Array) the cache id which you want to load.
	        */
        cache.loadCache = function(cacheIds, scope, callback) {
            if(!cacheIds || !(cacheIds instanceof Array)) {
                return;
            }
			if(!cache.serverCache){
				cache.serverCache = ux.megatron.Main.getServer().getCache();
				cache._selectProperty = ux.megatron.Main.getServer().getDynamicProperty("@cdb@Select Default Text");
			}
            try{
                var _cache = cache.getInstance();
                _cache.cacheIdCount = cacheIds.length;
                _cache.callbackScope = scope || this;
                if(callback && typeof(callback) == 'function') { 
                    _cache.callbackFunction = callback;
                }
                ux.megatron.util.Dwr.beginBatch();
                for(var i=0;i<_cache.cacheIdCount;i++){
                    var cacheId = cacheIds[i];
                    var childObj = cache.commonCache[cacheId];
/* @JSC :: begin DEBUG */
                    cache.log('CommonCache#loadCache cacheId[' + cacheId + '], cacheObj:' + childObj);
/* @JSC :: end */
                    if(childObj && childObj.cdf && childObj.cdf.toString() != '<data jsxid="jsxroot"/>') {
/* @JSC :: begin DEBUG */
                        cache.log('CommonCache#loadCache cacheObj.cdf : ' + childObj.cdf.toString() + ' :[' + (childObj.cdf.toString() != '<data jsxid="jsxroot"/>') + ']');
/* @JSC :: end */
                        _cache._setCallback();
                    }else{
/* @JSC :: begin DEBUG */
                        cache.log('CommonCache#loadCache load cacheObj.cdf ...');
/* @JSC :: end */
                        cache.commonCache[cacheId] = {name:"CommonCache_" + cacheIds[i], cdf:null};
/* @JSC :: begin DEBUG */
                        cache.log('CommonCache#loadCache _loadCommonData("' + cacheIds[i] + '", "' + cacheId + '");');
/* @JSC :: end */
                        cache.getInstance()._loadCommonData(cacheId);
                    }
                }
                ux.megatron.util.Dwr.endBatch();
            }catch(e){
                cache.log('CommonCache#loadCache Error : ' + e.message);
                throw e;
            }
        };
        
		/**
		* get object of common cache.
		* @params cacheId(String) the cache id of the object which you want.
		*/
        cache.prototype.getCache = function(cacheId) {
            if(!cacheId || cacheId.indexOf('CommonCache_') !== 0) {
                throw new jsx3.lang.IllegalArgumentException('parameters must be a string and start with "CommonCache_"!', 'cacheId : "' + cacheId + '"');
            }
            var cacheCdf = cache.commonCache[cacheId.replace('CommonCache_','')].cdf;
            if(!cacheCdf || !(cacheCdf instanceof jsx3.xml.CDF.Document))
				cacheCdf = jsx3.xml.CDF.Document.newDocument();
/* @JSC :: begin DEBUG */
            cache.log('CommonCache#getCache[' + cacheId + '] : ' + cacheCdf);
/* @JSC :: end */
            return cacheCdf.cloneDocument();
        };

        cache.mock = false;
        cache.prototype._loadCommonData = function(dataType) {
            if(ux.megatron.Main.isFileSystem()) { return; }
            if(cache.mock === true){
                cache.getInstance()["_load"+dataType](dataType);
                return;
            }
            var dwrClass = 'BaseUCC';
            var dwrFunction = 'getCodeListByCodeType';
			var dwrParam = [dataType];
            if(dataType == 'INTERNAL_ORG'){
                dwrClass = 'RetailInfoQueryUCC';
                dwrFunction = 'getInitInternalOrg';
                dwrParam = [];
            }
            ux.megatron.util.Dwr.callDwr(dwrClass, dwrFunction, dwrParam, this,
                function(data){
                    if (data !== null && typeof data == 'object') {
/* @JSC :: begin DEBUG */
                        if(window.dwr && dwr.util) { cache.log(dwr.util.toDescriptiveString(data, 5)); }
/* @JSC :: end */
                        this._convert(data, dataType);
                    }
                });
        };
        
        cache.prototype._convert = function(data, dataType) {
            if(!data || !dataType) {
                throw new jsx3.lang.IllegalArgumentException('parameters required!', 'dataType : ' + dataType + ',data : ' + data);
            }
            cache.commonCache[dataType].cdf = jsx3.xml.CDF.Document.newDocument();
			cache.commonCache[dataType].cdf.insertRecord({jsxid:'', jsxtext:cache._selectProperty, jsxstyle:'width:100%;'},null,false);

			var _data = data.content.data, recordList = [], i, item;
			var l = _data.length;
			if(dataType == 'INTERNAL_ORG'){
				for(i = 0; i < l; i++){
					item = _data[i];
					var tmpRecord = {
						jsxid:			item.internalOrgId, 
						jsxtext:		item.orgName, 
						cooperOrgId:	item.orgLevelCode,
						cooperOrgName:	item.orgTypeCode,
						userStatus:		item.internalOrgStatusCode
					};
	                cache.commonCache[dataType].cdf.insertRecord(tmpRecord,null,false);
					recordList.push(tmpRecord);
				}
			}else{
				for(i = 0; i < l; i++){
					item = _data[i];
					var tmpRecord = {jsxid:item.codeValue, jsxtext:item.codeName};
	                cache.commonCache[dataType].cdf.insertRecord(tmpRecord,null,false);
					recordList.push(tmpRecord);
				}
			}
/* @JSC :: begin DEBUG */
			if(window.dwr && dwr.util) { cache.log(dwr.util.toDescriptiveString(recordList, 5)); }
/* @JSC :: end */
			
            cache.serverCache.clearById(cache.commonCache[dataType].name);
            cache.serverCache.setDocument(cache.commonCache[dataType].name, cache.commonCache[dataType].cdf);
/* @JSC :: begin DEBUG */
            cache.log('CommonCache#_convert data['+dataType+'] : ' + cache.commonCache[dataType].cdf);
/* @JSC :: end */
            this._setCallback();
        };

        cache.prototype._setCallback = function() {
            this.cacheIdStep++;
/* @JSC :: begin DEBUG */
            cache.log('cacheIdStep : ' + this.cacheIdStep + ' | cacheIdCount : ' + this.cacheIdCount);
            cache.log('step over : ' + (this.cacheIdStep === this.cacheIdCount));
/* @JSC :: end */
           if(this.cacheIdStep === this.cacheIdCount){
                this.cacheIdStep = 0;
                this.callbackFunction.call(this.callbackScope);
/* @JSC :: begin DEBUG */
				cache.log('do callback function : ' + this.callbackFunction);
/* @JSC :: end */
            }
        };

        cache.log = function(msg, level){
			if(!this.LOG){
				this.LOG = jsx3.util.Logger.getLogger("ux.cbd.admin.CommonCache");
			}
			if(this.LOG.isLoggable(jsx3.util.Logger.DEBUG)){
				this.LOG.debug(msg);
			}
		};
		
		ux.cdb.admin.Log.info('CDB Admin CommonCache loading...');
    }
);